import util from '@/libs/util';
import Cookies from 'js-cookie';

export default {
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '教材名称',
                key: 'name'
            }, {
                title: '价格（元）',
                key: 'price'
            }, {
                title: '备注',
                key: 'remark'
            },{
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.detail(params.row.id);
                                }
                            }
                        }, '查看'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row.id);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {},
            query: {},
            selectYesOrNo: [{id: 'YES', name: '是'}, {id: 'NO', name: '否'}],

            total: 0,
            pageIndex: 1,
            pageSize: 10,

            editModal: false,
            editTitle: '',
            isRead: false,
            formDataRules: {// 添加班级表单验证规则
                name: [
                    { required: true, message: '请输入教材名称', trigger: 'blur' }
                ],
                price: [
                    { required: true, message: '请输入价格', trigger: 'blur', type: 'number'},
                    { required: true, validator: this.inputPrice, trigger: 'blur'}
                ]
            }
        };
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/book/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                organizeid: this.baseuser.orgId,
                name: this.query.name ? this.query.name : ''
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        refresh () {
            this.query = {};
            this.loadList();
        },
        showAdd () {
            this.editTitle = '新增教材';
            this.formData = {
                organizeid: this.baseuser.orgId,
                status: 'YES',
                price: 0
            };
            this.editModal = true;
            this.isRead = false;
        },
        detail (id) {
            this.editTitle = '教材信息';
            util.ajax.post('/busine/book/detail', util.toFormData({
                id: id
            })).then(res => {
                this.formData = res.data.data;
                this.editModal = true;
                this.isRead = true;
            });
        },
        showEdit (id) {
            this.editTitle = '修改教材';
            util.ajax.post('/busine/book/detail', util.toFormData({
                id: id
            })).then(res => {
                this.formData = res.data.data;
                this.editModal = true;
                this.isRead = false;
            });
        },
        delete (id) {
            this.$Modal.confirm({
                content: '是否确认删除？',
                onOk: () => {
                    util.ajax.post('/busine/book/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                            this.editModal = false;
                        }
                    });
                }
            });
        },
        formValidate () {
            let boo = false;
            let content = '';
            if (!this.formData.name) {
                boo = true;
                content = '教材名称不能为空';
            }
            if (boo) {
                this.$Modal.error({
                    content: content
                });
                return false;
            }
            return true;
        },
        save () {
            this.$refs['formData'].validate((valid) => {
                if (valid) {
                    let formData = this.formData;
                    formData.price = formData.price ? formData.price : 0;
                    util.ajax.post('/busine/book/save', util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('保存成功！');
                            this.loadList();
                            this.editModal = false;
                        }
                    });
                } else {
                    this.$Message.warning('请检查输入项！');
                }
            });
        },
        inputPrice: function (rule, value, callback, source) {
            let price = this.formData.price;
            if (price < 0) {
                callback(new Error('价格不得为负数！'));
            } else {
                callback();
            }
        }
    }
};
